<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="CrossFactory.SiteNote" %>
<%@ Import Namespace="CrossFactory.SiteNote.Components" %>
<%@ Import Namespace="CrossFactory.SiteNote.Hierarchy" %>
<%@ Import Namespace="CrossFactory.SiteNote.Security" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">    
  
</script>   
     
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
     <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="-1">
    <script language="CS" runat="server" >    
        void Page_Load(object sender, System.EventArgs e)
        {
            //System.Diagnostics.Debugger.Break();
            var bloc = HttpContext.Current.Request.QueryString["bloc"];
            var url = HttpContext.Current.Request.QueryString["url"];
            var contentId = HttpContext.Current.Request.QueryString["content"];
            if (string.IsNullOrEmpty(bloc))
            {
                bloc = "SiteNoteBlocContent_Content";
            }
            if (url != null)
            {
                SiteNotePage page = CrossFactory.SiteNote.Components.Settings._settings.HierarchyProvider.GetPageWithPath(url);
                if (page != null)
                {
                    ulong permission = page.SiteNotePageInfo.IsDraft ? Permissions.ReadOff : Permissions.ReadOn;
                    if (page.HasPermission(permission))
                    {
                        if (page.SiteNotePageInfo.BlocContents.ContainsKey(bloc))
                        {
                            if (string.IsNullOrEmpty(contentId))
                            {
                                foreach (var content in page.SiteNotePageInfo.BlocContents[bloc].Contents.Values)
                                {
                                    if (content is CrossFactory.SiteNote.Contents.XmlEditor)
                                    {
                                        var xml = ((CrossFactory.SiteNote.Contents.XmlEditor)content).Xml;
                                        string xmlString = ((CrossFactory.SiteNote.Contents.XmlEditor)content).Xml_String;
                                        xmlString = xmlString.Replace(".off.aspx", ".aspx");
                                        xmlString = xmlString.Replace("/Files.Off/", "/Files/").Replace("%2FFiles.Off%2F", "%2FFiles%2F");
                                        xml.LoadXml(xmlString);
                                        LoadRssResponse(xml);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                CrossFactory.SiteNote.Contents.XmlEditor c = (CrossFactory.SiteNote.Contents.XmlEditor)page.SiteNotePageInfo.BlocContents[bloc].Contents[contentId];
                                string xmlString = c.Xml_String;
                                xmlString = xmlString.Replace(".off.aspx", ".aspx");
                                xmlString = xmlString.Replace("/Files.Off/", "/Files/").Replace("%2FFiles.Off%2F", "%2FFiles%2F");
                                var xml = c.Xml;
                                xml.LoadXml(xmlString);
                                LoadRssResponse(xml);
                            }
                        }
                    }
                    else
                    {
                        Response.Clear();
                        Response.StatusCode = 401;
                        Response.End();
                    }
                }
            }
        }
        
        private static readonly XslCompiledTransform RssFeedTransform = LoadRssFeedTransform();

        static XslCompiledTransform LoadRssFeedTransform()
        {
            var result = new XslCompiledTransform();

            
            var xslt = File.ReadAllText(CrossFactory.SiteNote.Components.Utils._MapPath(CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.VirtualPath + "/XmlEditor/ConvertToRss.xslt"));
            
            xslt = xslt.Replace("%URLGlobalXslt%", CrossFactory.SiteNote.Components.Utils._MapPath(CrossFactory.SiteNote.Components.Utils._ApplicationPath + CrossFactory.SiteNote.Components.Settings._settings.VirtualPath + "/XmlEditor/Global.xslt"));

            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(xslt);
            MemoryStream mStream = new MemoryStream(byteArray, 0, byteArray.Length);
                    
            using (var xsltStream = XmlReader.Create(mStream))
                result.Load(xsltStream, new XsltSettings(false, true), new XmlUrlResolver());

            return result; 
        }
      
        protected void LoadRssResponse(XmlDocument xml)
        {
            var args = new XsltArgumentList(); 

            Response.Clear();
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Expires = 0;
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");
            Response.ContentType = "text/xml";
            
            args.AddParam("link", "", CrossFactory.SiteNote.Components.Utils._RequestUrlClient + HttpContext.Current.Request.QueryString["url"]);
            args.AddParam("applicationPath", "", CrossFactory.SiteNote.Components.Utils._ApplicationPath);
            using (var stream = new MemoryStream())
            {
                lock (RssFeedTransform)
                    
                    RssFeedTransform.Transform(xml, args, stream);

                stream.Position = 0;
                var buffer = new byte[0x1000];
                int length;

                while ((length = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    var s = System.Text.UTF8Encoding.UTF8.GetString(buffer, 0, length);
                    s = s.Replace("CDATATagStart", "<![CDATA[").Replace("CDATATagEnd", "]]>");
                    Response.Write(s);
                }
            }

            Response.Flush();
            Response.End();
        }
  </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
